﻿/*
    Parker for Kaixin001.com 

    Copyright (C) 2008, TurboZV, turbozv@gmail.com, http://www.turbozv.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.IO;

namespace Helper
{
    public static class TraceLog
    {
        public static void Print(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Gray;
            if (_newLine)
            {
                string str = string.Format("[{0}]: ", dt);
                Console.Write(str);
                WriteToLogFile(str);
                _newLine = false;
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(msg);
            WriteToLogFile(msg);
            Console.ForegroundColor = color;
        }

        public static void PrintLn(string format, params object[] args)
        {
            Print(format, args);
            WriteToLogFile("\r\n");
            Console.WriteLine();
            _newLine = true;
        }

        public static void Error(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            if (!_newLine)
            {
                WriteToLogFile("\r\n");
                Console.WriteLine();
            }
            string str = string.Format("[{0}]: {1}", dt, msg);
            WriteToLogFile(str + "\r\n");
            Console.WriteLine(str);
            Console.ForegroundColor = color;
            _newLine = true;
        }

        private static string GetMessage(string format, object[] args)
        {
            if (args.Length > 0)
            {
                return string.Format(format, args);
            }
            else
            {
                return format;
            }
        }

        private static void WriteToLogFile(string str)
        {
            if (!WriteToFile)
            {
                return;
            }
            if (string.IsNullOrEmpty(str))
            {
                return;
            }

            str = str.Replace("\b", "");
            StreamWriter sw = new StreamWriter(c_LogFile, true);
            sw.Write(str);
            sw.Flush();
            sw.Close();
        }

        public const string c_LogFile = "log.txt";
        private static bool _newLine = true;
        public static bool WriteToFile = false;
    }
}